package terra.ui;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.GUIContext;

import terra.ui.wnd.AbstractWindow;

public class GUI
{
	public static final int iconSize = 32;
	public static final int inventoryItemsWidth = 6;
	public static final int tooltipWidth = 128;
	public static final int craftWidth = iconSize*4;
	public static final int craftHeight = iconSize*5 + 16;
	public static final int sizeOfTitle = 150;
	public static final int gap = 4;
	public static final int craftGap = 8;
	public static final int wearGap = 12;
	public static final int wearWidth = iconSize*3 + wearGap*3;
	public static Color back = new Color(Color.gray.getRed(),Color.gray.getGreen(),Color.gray.getBlue(),0.65f);
	public static ArrayList<AbstractWindow> ui = new ArrayList<AbstractWindow>();
	
	public static boolean guiHasFocus() {
		for(AbstractWindow wnd : ui)
			if (wnd.isVisible() && wnd.isOver())
				return true;
		
		return false;
	}
	
	public static AbstractWindow hasFocus() {
		for(AbstractWindow wnd : ui)
			if (wnd.isVisible() && wnd.hasFocus())
				return wnd;
		
		return null;
	}

	public static AbstractWindow getFocusedWnd() {
		return ui.get(0);
	}
	
	public static void update(int delta) {
		for(AbstractWindow wnd : ui)
			wnd.update(delta);
	}
	
	public static void render(GUIContext container,Graphics g) {
		try {
			for(int i = ui.size()-1 ; i >=0;i--){
				AbstractWindow wnd = ui.get(i);
				if (wnd.isVisible())
					wnd.render(container, g);
			}	
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public static void addWnd(AbstractWindow wnd) {
		if (!ui.contains(wnd))
			ui.add(wnd);
	}	
	
	public static void removeWnd(AbstractWindow wnd) {
		if (ui.contains(wnd))
			ui.remove(wnd);
	}	
	
	public static void giveFocus(AbstractWindow wnd) {
		if (hasFocus() != null)
			hasFocus().focusLost();
		GUI.ui.remove(wnd);
		GUI.ui.add(0,wnd);
		wnd.setFocus(true);
	}
}
